﻿CREATE PROCEDURE [dbo].[yaf_topic_prune](
                @ForumID INT  = NULL,
                @Days    INT)
AS
    BEGIN
        DECLARE  @c CURSOR
        DECLARE  @TopicID INT
        DECLARE  @Count INT
        SET @Count = 0
        IF @ForumID = 0
        SET @ForumID = NULL
        IF @ForumID IS NOT NULL
        BEGIN
            SET @c = CURSOR FOR SELECT TopicID
                                FROM   yaf_Topic
                                WHERE  ForumID = @ForumID
                                AND Priority = 0
                                AND Datediff(dd,LastPosted,Getdate()) > @Days
        END
        ELSE
        BEGIN
            SET @c = CURSOR FOR SELECT TopicID
                                FROM   yaf_Topic
                                WHERE  Priority = 0
                                AND Datediff(dd,LastPosted,Getdate()) > @Days
        END
        OPEN @c
        FETCH  @c
        INTO @TopicID
        WHILE @@FETCH_STATUS = 0
        BEGIN
            EXEC yaf_topic_delete
                 @TopicID ,
                 0
            SET @Count = @Count + 1
            FETCH  @c
            INTO @TopicID
        END
        CLOSE @c
        DEALLOCATE @c
        -- This takes forever with many posts...
        --exec yaf_topic_updatelastpost
        SELECT COUNT = @Count
    END


